{{- define "snapshot_controller_resources" }}
cpu: 10m
memory: 25Mi
{{- end }}

{{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: snapshot-controller
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "snapshot-controller")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: snapshot-controller
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: "snapshot-controller"
      minAllowed:
        {{- include "snapshot_controller_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 20m
        memory: 50Mi
    {{- include "helm_lib_vpa_kube_rbac_proxy_resources" . | nindent 4 }}
{{- end }}
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: snapshot-controller
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "snapshot-controller" )) | nindent 2 }}
spec:
  minAvailable: {{ include "helm_lib_is_ha_to_value" (list . 1 0) }}
  selector:
    matchLabels:
      app: snapshot-controller
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: snapshot-controller
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "snapshot-controller")) | nindent 2 }}
spec:
  minReadySeconds: 15
  revisionHistoryLimit: 2
  {{- include "helm_lib_deployment_strategy_and_replicas_for_ha" . | nindent 2 }}
  selector:
    matchLabels:
      app: snapshot-controller
  template:
    metadata:
      labels:
        app: snapshot-controller
    spec:
      imagePullSecrets:
        - name: deckhouse-registry
      {{- include "helm_lib_priority_class" (tuple . "cluster-low") | nindent 6 }}
      {{- include "helm_lib_node_selector" (tuple . "system") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "system") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_nobody" . | nindent 6 }}
      {{- include "helm_lib_pod_anti_affinity_for_ha" (list . (dict "app" "snapshot-controller")) | nindent 6 }}
      containers:
      - name: snapshot-controller
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem_capabilities_drop_all" . | nindent 8 }}
        args:
        - --v=5
        - --leader-election=true
        - --http-endpoint=127.0.0.1:8080
        image: {{ include "helm_lib_module_image" (list . "snapshotController") }}
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
            {{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "snapshot_controller_resources" . | nindent 12 }}
            {{- end }}
      - name: kube-rbac-proxy
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem_capabilities_drop_all" . | nindent 8 }}
        image: {{ include "helm_lib_module_common_image" (list . "kubeRbacProxy") }}
        args:
          - "--secure-listen-address=$(KUBE_RBAC_PROXY_LISTEN_ADDRESS):8080"
          - "--v=2"
          - "--logtostderr=true"
          - "--stale-cache-interval=1h30m"
        ports:
        - containerPort: 8080
          name: https-metrics
        env:
          - name: KUBE_RBAC_PROXY_LISTEN_ADDRESS
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: KUBE_RBAC_PROXY_CONFIG
            value: |
              upstreams:
              - upstream: http://127.0.0.1:8080/metrics
                path: /metrics
                authorization:
                  resourceAttributes:
                    namespace: d8-{{ .Chart.Name }}
                    apiGroup: apps
                    apiVersion: v1
                    resource: deployments
                    subresource: prometheus-metrics
                    name: snapshot-controller
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_only_logs" . | nindent 12 }}
            {{- if not ( .Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "helm_lib_container_kube_rbac_proxy_resources" . | nindent 12 }}
            {{- end }}
      serviceAccountName: snapshot-controller
